package com.carl.mall.ware.service.impl;

import com.alibaba.fastjson.JSON;
import com.carl.common.dto.SkuHasStockDto;
import com.carl.common.utils.R;
import com.carl.mall.ware.feign.ProductFeignService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.common.utils.PageUtils;
import com.carl.common.utils.Query;

import com.carl.mall.ware.dao.WareSkuDao;
import com.carl.mall.ware.entity.WareSkuEntity;
import com.carl.mall.ware.service.WareSkuService;

@Slf4j
@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {


    @Autowired
    private WareSkuDao wareSkuDao;

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)){
            wrapper.and((wrappers) ->{
                wrappers.eq("sku_id",skuId);
            });
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)){
                wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
          //先查一下有没有入库记录 必须sku_id和ware_id两个条件都满足才行
        List<WareSkuEntity> skuEntities = wareSkuDao
                .selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (skuEntities == null || skuEntities.isEmpty()){
            //如果没有则新增数据
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            //调远程商品服务获取sku name
            try {
                R info = productFeignService.info(skuId);
                Map<String,Object> data= (Map<String, Object>) info.get("skuInfo");
                if (info.getCode() == 0){
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            wareSkuDao.insert(wareSkuEntity);

        }
        wareSkuDao.addStock(skuId,wareId,skuNum);

    }

    /**
     * 获取每个skuId的库存
     * @param skuIds
     * @return
     */
    @Override
    public List<SkuHasStockDto> getHasStock(List<Long> skuIds) {
        if (log.isInfoEnabled()){
            log.info("WareSkuServiceImpl.getHasStock.skuIds:{}", JSON.toJSONString(skuIds));
        }
        List<SkuHasStockDto> stockDtos = skuIds.stream().map((skuId) -> {
            Long count = baseMapper.getSkuStock(skuId);
            SkuHasStockDto stockDto = new SkuHasStockDto();
            stockDto.setSkuId(skuId);
            stockDto.setHasStock(count > 0);
            return stockDto;
        }).collect(Collectors.toList());
        return stockDtos;
    }




























}